package dao;

import com.sun.org.apache.xpath.internal.operations.String;
import model.User;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao  {//自己demo
    //返回所有的用户
    public List<User> getAllUser(){
        ArrayList<User> userArrayList = new ArrayList<User>();
        String sql="select * from user";
        try{
        ResultSet rs=this.executeQuery(sql,null);
        while(rs.next()){
            User user = new User();
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setRole(rs.getInt("role"));
            userArrayList.add(user);


        }



    }catch (SQLException e){
            e.getMessage();
        }finally{
            this.closeAll();
        }
        return userArrayList;
    }

    //根据用户名查找用户
    public User getUserByName(){
        User user=null;
        try {
            String sql="select * from user where username=?";
            ResultSet rs=this.exectuQuery(sql,new String[]{name});
            if(rs.next()){
                user=new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getInt("role"));
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.closeAll();

        }
     return  user;

    }
//添加新用户
    public boolean addUser(User user){
        boolean r=false;
        try {

            String sql="insert into user(username,password,role) values(?,?,?)";
            int rs=this.executeUpdate(sql,new String[]{user.getUsername(),user.getPassword(),user.getRole()});
            if(rs>0){
                r=true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
return r;
    }



}
